#!/usr/bin/python
from zplot import *
import xml.etree.ElementTree as ET
import parse
import collections

# 614 ways have names, 1223

def main():
    data = parse.Parse('data/lowell.xml')
    good = 0
    bad = 0
    unique = collections.defaultdict(int)

    for w in data.ways:
        if w.name:
            good += 1
            unique[w.name] += 1
        else:
            bad += 1
    print good, len(unique), bad

    buckets = [0 for x in range(max(unique.values())+1)]
    for cnt in unique.values():
        buckets[cnt] += 1
    print buckets

    c = postscript(title='ways-per-road.eps', dimensions=[400,400])
    cr = canvasregion(canvas=c, coord=[40,40], dimensions=[350,350])
    d = drawable(canvasreg=cr, xrange=[0,len(buckets)], yrange=[0,max(buckets)])
    axis(drawable=d, style='xy')
    t = table()
    t.addcolumn('ways')
    t.addcolumn('roads')
    for ways in range(len(buckets)):
        roads = buckets[ways]
        t.insert({'ways': ways, 'roads': roads})
    t.dump()
    p = plotter()
    p.verticalbars(drawable=d, table=t, xfield='ways', yfield='roads', barwidth=0.8, fill=True)

    c.render()


if __name__ == '__main__':
    main()
